package copyengine.ui.component.list
{
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.utils.getTimer;

	import copyengine.utils.GeneralUtils;
	import copyengine.utils.debug.CELog;

	public final class CEMListTouchProxy
	{
		private var listTargetMc:MovieClip;
		private var direction:int;
		private var moveOperater:ICEMListMoveOperater;

		private var isTouchStart:Boolean=false;
		private var lastCheckPos:Number;
		private var mouseDownCheckPos:Number;
		private var mouseDownTime:int

		public function CEMListTouchProxy()
		{
		}

		public function initialize(_listTargetMc:MovieClip, _direction:int, _moveOperater:ICEMListMoveOperater):void
		{
			listTargetMc=_listTargetMc;
			direction=_direction;
			moveOperater=_moveOperater;
			addListener();
		}

		private function addListener():void
		{
			GeneralUtils.addTargetEventListener(listTargetMc, MouseEvent.ROLL_OUT, onMouseUpListTarget);
			GeneralUtils.addTargetEventListener(listTargetMc, MouseEvent.MOUSE_UP, onMouseUpListTarget);
			GeneralUtils.addTargetEventListener(listTargetMc, MouseEvent.MOUSE_DOWN, onMouseDownListTarget);
			GeneralUtils.addTargetEventListener(listTargetMc, MouseEvent.MOUSE_MOVE, onMouseMoveListTarget);
		}

		private function removeListener():void
		{
			GeneralUtils.removeTargetEventListener(listTargetMc, MouseEvent.ROLL_OUT, onMouseUpListTarget);
			GeneralUtils.removeTargetEventListener(listTargetMc, MouseEvent.MOUSE_UP, onMouseUpListTarget);
			GeneralUtils.removeTargetEventListener(listTargetMc, MouseEvent.MOUSE_DOWN, onMouseDownListTarget);
			GeneralUtils.removeTargetEventListener(listTargetMc, MouseEvent.MOUSE_MOVE, onMouseMoveListTarget);
		}

		private function onMouseUpListTarget(e:MouseEvent):void
		{
			if (isTouchStart)
			{
				isTouchStart=false;
				var currentCheckPos:Number=direction == CEMListCore.LAYOUT_HORIZONTAL ? e.stageX : e.stageY;
				onTouchCancel(currentCheckPos - mouseDownCheckPos);
			}
		}

		private function onMouseDownListTarget(e:MouseEvent):void
		{
			isTouchStart=true
			mouseDownCheckPos=lastCheckPos=direction == CEMListCore.LAYOUT_HORIZONTAL ? e.stageX : e.stageY;
			mouseDownTime=getTimer();
			
			onTouchStart();
		}

		private function onMouseMoveListTarget(e:MouseEvent):void
		{
			if (isTouchStart)
			{
				var currentCheckPos:Number=direction == CEMListCore.LAYOUT_HORIZONTAL ? e.stageX : e.stageY;
				if (currentCheckPos != lastCheckPos)
				{
					onTouchMove(currentCheckPos - mouseDownCheckPos);
					lastCheckPos=currentCheckPos;
				}
			}
		}

		//========================//
		//==Private  Function
		//=========================//

		private function onTouchStart():void
		{
		}

		private function onTouchCancel(_moveOffset:Number):void
		{
			CELog.log("Cancel offset is : " + _moveOffset);
		}

		private function onTouchMove(_moveOffset:Number):void
		{
			//CELog.log("Move offset is : " + _moveOffset)
		}


	}
}
